package com.zretc.dao.impl;

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zretc.dao.PlayListDao;
import com.zretc.entity.Area;
import com.zretc.entity.PlayList;
import com.zretc.entity.Singer;
import com.zretc.entity.Song;
import com.zretc.entity.SongType;
import com.zretc.util.DBUtil;

public class PlayListDaoImpl implements PlayListDao {

	@Override
	public List<Song> findPlaySongByAccountId(String accountId) {
		List<Song> list = new ArrayList<>();
		ResultSet rs = null;
		Song song = null;
		Singer singer = null;
		SongType songType = null;
		Area area = null;
		if (accountId==null) {
			return null;
		}else {
			rs = DBUtil.doQuery("select C.account_id,A.song_id,A.song_name,A.song_time,A.click_count,A.song_status,B.singer_id,A.song_text,A.publish_date,"
					+ "	B.singer_name,B.gender,B.singer_picture"
					+ " from song A left join singer B on A.singer_id = B.singer_id left join playlist C "
					+ " on C.song_id = A.song_id where C.account_id = ?",accountId);
		}
		
		try {
			while (rs.next()) {
				Integer songId = rs.getInt("song_id");
				String songName = rs.getString("song_name");
				String songTime = rs.getString("song_time");
				String publishDate = rs.getString("publish_date");
				Integer clickCount = rs.getInt("click_count");
				Integer songStatus = rs.getInt("song_status");
				Integer singerId = rs.getInt("singer_id");
				String songText = rs.getString("song_text");
				String singerName = rs.getString("singer_name");
				String gender = rs.getString("gender");
				String singerPicture = rs.getString("singer_picture");
				singer = new Singer(singerId, singerName, gender, singerPicture, area);
				song = new Song(songId, songName, songTime, clickCount, songStatus, songType, singer, songText, publishDate);
				list.add(song);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Song findSongBySongName(String songName) {
		Song song = null;
		Singer singer = null;
		SongType songType = null;
		Area area = null;
		ResultSet rs = DBUtil.doQuery("select A.song_id,A.song_name,A.song_time,A.click_count,A.song_status,B.singer_id,A.song_text,A.publish_date,"
				+ "	B.singer_name,B.gender,B.singer_picture"
				+ " from song A left join singer B on A.singer_id = B.singer_id"
				+ " where A.song_name = ?",songName);
		try {
			while (rs.next()) {
				Integer songId = rs.getInt("song_id");
				String songTime = rs.getString("song_time");
				String publishDate = rs.getString("publish_date");
				Integer clickCount = rs.getInt("click_count");
				Integer songStatus = rs.getInt("song_status");
				Integer singerId = rs.getInt("singer_id");
				String songText = rs.getString("song_text");
				String singerName = rs.getString("singer_name");
				String gender = rs.getString("gender");
				String singerPicture = rs.getString("singer_picture");
				singer = new Singer(singerId, singerName, gender, singerPicture, area);
				song = new Song(songId, songName, songTime, clickCount, songStatus, songType, singer, songText, publishDate);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return song;
	}

	@Override
	public int deletePlaySong(Integer songId, Integer accountId) {
		int n = DBUtil.doUpdate("delete from playlist where song_id = ? and account_id = ?", songId,accountId);
		return n;
	}

	@Override
	public int insertPlayBySongIdAndAccountId(Integer songId, Integer accountId) {
		int n = DBUtil.doUpdate("insert into playlist values(?,?)", accountId,songId);
		return n;
	}

	@Override
	public PlayList findBySongIdAndAccountId(Integer songId, Integer accountId) {
		PlayList playList = null;
		ResultSet rs = DBUtil.doQuery("select count(*) from playlist where account_id = ? and song_id = ?", accountId,songId);
		Integer total = 0;
		try {
			while (rs.next()) {
				total = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (total!=0) {
			playList = new PlayList(accountId, songId);
		}
		return playList;
	}
	
}
